package com.example.batterynotification;

import android.os.Bundle;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
	public static Context contextOfApplication;
	private AppContext appContext;
	batteryLevelReceiver batInfo;
	BatteryReceiverForeground batForeGround;
	IntentFilter batIntentFilter;
	private Intent mIntent;
	ComponentName componentName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initialize();
		
	}

	private void initialize() {
		Foreground.init(getApplication());
		batForeGround = new BatteryReceiverForeground();
		batIntentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		appContext = new AppContext(this);
		new Preferences(appContext);
		Preferences.savePreferences("isMainActivityActive", true);
		new ManageNotifications(appContext);
		BatteryReceiverForegroundResources.setACChargingTimerStarted(false);
		BatteryReceiverForegroundResources.setDischargingTimerStarted(false);
		BatteryReceiverForegroundResources.setUSBChargingTimerStarted(false);

		if ((Preferences.getBoolPreferences("HighSwitch") || (Preferences
				.getBoolPreferences("LowSwitch")))) {
			Log.i("LC", "In condition");
			mIntent = new Intent(getApplicationContext(),
					ListeningService.class);
			componentName = startService(mIntent);
//			ManageNotifications.createNotification();
		} else {
			if (componentName != null)
				stopService(mIntent);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.i("LC", "In the menu  " + item.getItemId());
		switch (item.getItemId()) {
		case R.id.action_settings:
			Log.i("LC", "Case done");
			Intent intent = new Intent(appContext.getAppContext(),
					MainSettingsActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Preferences.savePreferences("isMainActivityActive", true);
		registerReceiver(batForeGround, batIntentFilter);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Preferences.savePreferences("isMainActivityActive", false);
		Log.i("LC", "MAIN ACTIVITY PAUSED");
		unregisterReceiver(batForeGround);
	}

	public void openSettings(View v) {
		Intent intent = new Intent(appContext.getAppContext(),
				MainSettingsActivity.class);
		startActivity(intent);

	}
//	public void testSwitch(View v)
//	{
//		Toast.makeText(getApplicationContext(), "did it", Toast.LENGTH_LONG).show();
//	}
	 int count=0;
	public void Test(View v)
	{
//		if(count==0)
//		{
//			count++;
//			String strRingtonePreference = Preferences
//			.getStringPreferences("HighRingTone"); 
//	Intent notificationSoundManagerIntent = new Intent(this,notificationSoundService.class);
//	notificationSoundManagerIntent.putExtra("sound", strRingtonePreference);
//	Log.i("NS", "the notificationService sound started");
//	startService(notificationSoundManagerIntent);
//		}
//		else
		{
			count=0;
//		String strRingtonePreference = Preferences
//				.getStringPreferences("HighRingTone"); 
		Intent notificationSoundManagerIntent = new Intent(this,notificationSoundService.class);
		//notificationSoundManagerIntent.putExtra("sound", strRingtonePreference);
		
		stopService(notificationSoundManagerIntent);
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// unregisterReceiver(batInfo);
	}

	@Override
    protected void onNewIntent(Intent intent) {

        MainActivity.this.finish();

        // -----Start New Dashboard  when notification is clicked----

        Intent i = new Intent(MainActivity.this, MainActivity.class);

        startActivity(i);

        super.onNewIntent(intent);
    }

}
